﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

namespace Mang24k99.Web.Analyst
{
    public partial class AnalystAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ID"]!=null)
            {
                this.txtName.Text=Session["ID"].ToString();
                this.txtPost.Text = "post";
            }

        }

        /// <summary>
        /// 预览并显示图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string filetype = this.FileUpload1.PostedFile.ContentType;
                int fileLength = this.FileUpload1.PostedFile.ContentLength;
                float maxSize = 5;

                //格式验证
                if (filetype != "image/pjpeg" && filetype != "image/jpeg" && filetype != "image/gif")
                {
                    string strMsg = "图片格式不对，请上传.jpg、.Jpeg或.gif格式图片！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return;
                }

                //文件大小验证
                if ((float)fileLength / 1024 > maxSize)
                {
                    string strMsg = "图片大小不能大于" + maxSize + "K！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return;
                }
                string fname = this.FileUpload1.FileName;       //得到文件名
                string savePath = Server.MapPath("images") + "\\" + fname;
                //if (System.IO.File.Exists(savePath))
                //{
                //    System.IO.File.Delete(savePath);
                //}
                this.FileUpload1.PostedFile.SaveAs(savePath);          //上传到服务器目录
                this.imgPic.ImageUrl = "images/" + fname;           //图片显示

                //读取上传文件流
                System.IO.Stream fs = FileUpload1.PostedFile.InputStream;
                byte[] ByteArray = new byte[fileLength];
                int nBytesRead = fs.Read(ByteArray, 0, fileLength);
                MemoryStream mBytes = new MemoryStream(ByteArray, 0, fileLength);
                fs.Close();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            ListItem newitem = new ListItem();
            newitem.Text = this.txtWord.Text;
            newitem.Value = this.txtWord.Text;
            this.lstWord.ClearSelection();
            newitem.Selected = true;
            this.lstWord.Items.Add(newitem);
            
            string strMsg =this.lstWord.SelectedValue;
            Common.MessageBox.Show(this.Page, strMsg);
        }

        /// <summary>
        /// 移除listbox的选中项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRemove_Click(object sender, EventArgs e)
        {
            this.lstWord.Items.Remove(lstWord.SelectedItem);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}
